package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.AcceptOrderBean;
import com.ymatou.operate.trading.api.service.AcceptOrderCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

public class Ts_AcceptOrder {
	private static AcceptOrderBean acceptorderbean;
	private static AcceptOrderCall acceptordercall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("卖家接单");
	}

	@Before
	public void caseUp() {
		acceptorderbean = new AcceptOrderBean();
		acceptordercall = new AcceptOrderCall();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(acceptordercall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	//数据库检查点
	private void verifyDb(){	    
        DefaultWapper database = new DefaultWapper();
        HashMap  m = new HashMap();
        m.put("OrderId", acceptorderbean.getOrderId());
        m.put("TradingStatus", 17);
        m.put("TraddingId", acceptorderbean.getOrderId());
        m.put("UserId", acceptorderbean.getSellerId());        
        Logger.verifyEquals(17, (Integer)database.selectOrder(m).get(0).get("iTradingStatus"), "验证订单表状态");
        Logger.verifyEquals(17, database.selectTradingStatus(m).get(0).get("iTradingStatus"), "验证交易表状态");
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AcceptOrder_001() {
		Logger.start(true,"卖家接单成功");
		try {
			// 创建订单
			Logger.comment("准备前置数据");
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);		
			Logger.comment("前置数据准备结束");
			// 接单
			acceptorderbean.setOrderId(orderId);// 订单ID
			acceptorderbean.setSellerId(sellerId);// 卖家ID
			// 发送请求
			acceptordercall.setData(acceptorderbean);
			acceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, acceptordercall.getCode(), "验证Code");
			// 数据库验证		
			 verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AcceptOrder_002() {
		Logger.start(false,"订单不存在，卖家接单失败");
		try {
			int orderId = 1048827;
			int SellerId = 288484;
			// 接单
			acceptorderbean.setOrderId(orderId);// 订单ID
			Logger.comment("SellerId:" + SellerId);
			acceptorderbean.setSellerId(SellerId);// 卖家ID
			// 发送请求
			acceptordercall.setData(acceptorderbean);
			acceptordercall.callService();
			Logger.verifyEquals(ECode.FAILED201, acceptordercall.getCode(), "验证Code");
			Logger.verifyEquals("订单号" + orderId + "不存在", acceptordercall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


}
